package File;
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 字节/字符输出流的示例
 */
public class TestDemo3 {
    public static void main(String[] args) {
        //由于每次输入输出都会清空，这里还有一个追加
        try(OutputStream outputStream=new FileOutputStream("MyTestFolder/MyTestFile.txt")){
            PrintWriter printWriter=new PrintWriter(outputStream);
            printWriter.println("你好");
            printWriter.println("学学哇");
            printWriter.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main3(String[] args) {
        //字符输出流输出文本文件更方便
        try(Writer writer=new FileWriter("MyTestFolder/MyTestFile.txt")){
            writer.write("因为有腻，温暖了死寂");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main2(String[] args) {
        //字节输出流输出文本文件
        try(OutputStream outputStream=new FileOutputStream("MyTestFolder/MyTestFile.txt")){
            String str="听我说谢谢你";
            outputStream.write(str.getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main1(String[] args) {
        //单个字符的依次输出方法
        try(OutputStream outputStream=new FileOutputStream("MyTestFolder/MyTestFile.txt")){
            outputStream.write('1');
            outputStream.write('2');
            outputStream.write('3');
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
